*******************************************************************************
* Analysis for Love, Carlin, and Singer
* Required packages: lassopack, spost13, cv_regress
* Note: Graphs use schemepack styles
*******************************************************************************/


*set graph scheme
set scheme s1color

* Set working directory and load data
cd "/Users/gregorylove/Library/CloudStorage/Dropbox/forecasting governor elections 2024/final submission/final final/replication data"

use "full data gov.dta", clear

********************************************************************************
* Initial Model Selection and Base Analysis
********************************************************************************

* LASSO model selection
lasso2 vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 
lasso2, lic(ebic)

* Selected model regression (Table 1)
reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 
	outreg2 using "table 1", noparen dec(3) label replace
cv_regress

* Null LOOV MAE
reg vote_share 
cv_regress

* Winner prediction models
lassologit winner l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 
logit winner l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 

* Winner forecast error analysis
predict yhat_winner if winner != .
gen yhat_winner_pr=yhat_winner
replace yhat_winner = 1 if yhat_winner > .5 & winner != .
replace yhat_winner = 0 if yhat_winner < .5 & winner != .
tab yhat_winner winner
display ((71 + 263) / 443) * 100

*Brier score
brier winner yhat_winner_pr 

**LOO accuracy for winner

* Create storage for predicted probabilities
gen loo_pred = .
gen loo_correct = .

* Loop through observations where winner is not missing
forvalues i = 1/`=_N' {
    if winner[`i'] != . {
        quietly {
            * Fit model excluding observation i
            logit winner l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
                c.l2.Approval_Smoothed_pres##pres_copartisan if _n != `i' & winner != .
                
            * Predict probability for left-out observation
            predict p if _n == `i'
            replace loo_pred = p if _n == `i'
            drop p
            
            * Check if prediction was correct (using 0.5 threshold)
            replace loo_correct = (loo_pred > 0.5 & winner == 1) | (loo_pred <= 0.5 & winner == 0) if _n == `i'
        }
        if mod(`i', 50) == 0 {
            display "Processed `i' observations"
        }
    }
}

* Calculate and display LOO accuracy
sum loo_correct if winner != .
display "Leave-one-out accuracy rate: " r(mean) * 100 "%"

* Clean up
drop loo_pred loo_correct

********************************************************************************
* Before-the-fact Errors Analysis
********************************************************************************

* Generate predictions for full model for each year
foreach l of num 1990/2019 {
    reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
        c.l2.Approval_Smoothed_pres##pres_copartisan if year < `l'
    predict yhat_vote_`l' if year == `l' & vote_share != .
}
egen yhat_vote = rowtotal(yhat_vote_1990-yhat_vote_2019), missing

* Generate predictions for simple model for each year
foreach l of num 1990/2019 {
    reg vote_share l2.c.Approval_Smoothed if year < `l'
    predict yhat_vote_simple_`l' if year == `l' & vote_share != .
}
egen yhat_vote_simple = rowtotal(yhat_vote_simple_1990-yhat_vote_simple_2019), missing

* Generate error metrics for full model
gen error_before = yhat_vote - vote_share if yhat_vote != .
gen abs_error_before = abs(yhat_vote - vote_share) if yhat_vote != .

* Generate error metrics for simple model
gen error_before_simple = yhat_vote_simple - vote_share if yhat_vote_simple != .
gen abs_error_before_simple = abs(yhat_vote_simple - vote_share) if yhat_vote_simple != .

* In-sample predictions for full model
reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan
predict yhat_vote_insample
gen error_insample = yhat_vote_insample - vote_share if yhat_vote != .
gen abs_error_insample = abs(yhat_vote_insample - vote_share) if yhat_vote != .

* In-sample predictions for simple model
reg vote_share l2.c.Approval_Smoothed
predict yhat_vote_insample_simple
gen error_insample_simple = yhat_vote_insample_simple - vote_share if yhat_vote_simple != .
gen abs_error_insample_simple = abs(yhat_vote_insample_simple - vote_share) if yhat_vote_simple != .

* Summarize error metrics for both models
sum error_insample abs_error_insample error_before abs_error_before 
sum error_insample_simple abs_error_insample_simple error_before_simple abs_error_before_simple

********************************************************************************
* Visualizations
********************************************************************************

* Figure 1A: Approval vs Vote Share
* Figure 1A: Approval vs Vote Share
graph twoway ///
    (scatter l2.Approval_Smoothed vote_share, mcolor(blue) msize(small)) ///
    (line vote_share vote_share, lcolor(gray) lpattern(solid) lwidth(thin)), ///
    xlabel(15(5)85, labsize(small) angle(45) grid) ///
    ylabel(15(5)85, labsize(small) grid) ///
    xtitle("Vote Share") ///
    ytitle("Gubernatorial Approval 2 Qtrs Pre-Election") ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    legend(off)

* Figure 1B: Predicted vs Actual Vote Share
graph twoway ///
    (scatter yhat_vote vote_share, mcolor(blue) msize(small)) ///
    (line vote_share vote_share, lcolor(gray) lpattern(solid) lwidth(thin)), ///
    xlabel(15(5)85, labsize(small) angle(0) grid) ///
    ylabel(15(5)85, labsize(small) grid) ///
    xtitle("Vote Share") ///
    ytitle("Before-the-Fact Predicted Vote Share") ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    legend(off)

********************************************************************************
* Alternative Model Analysis - Approval Only
********************************************************************************

use "full data gov.dta", clear

foreach l of num 1990/2019 {
    reg vote_share l2.c.Approval_Smoothed if year < `l'
    predict yhat_vote_`l' if year == `l' & vote_share != .
}

egen yhat_vote = rowtotal(yhat_vote_1990-yhat_vote_2019), missing
gen error_before = yhat_vote - vote_share if yhat_vote != .
gen abs_error_before = abs(yhat_vote - vote_share) if yhat_vote != .

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan
predict yhat_vote_insample
gen error_insample = yhat_vote_insample - vote_share if yhat_vote != .
gen abs_error_insample = abs(yhat_vote_insample - vote_share) if yhat_vote != .

sum error_insample abs_error_insample error_before abs_error_before, detail

********************************************************************************
* 2022 State-by-State Forecasts
********************************************************************************
**Table A5**
reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan
 *Alabama*
quietly mtable, at(l2.Approval_Smoothed= 57 Incumbcode= 1  gov_party=2 dem_per_two_party=37 pres_copartisan=0 l2.Approval_Smoothed_pres=32) statistics (est ul ll) rown(Alabama) dec(2)
*Alaska
quietly mtable, at(l2.Approval_Smoothed= 52 Incumbcode= 1  gov_party=2 dem_per_two_party=44.7 pres_copartisan=0 l2.Approval_Smoothed_pres=35) statistics (est ul ll) below rown(Alaska) dec(2)
*Arizona
quietly mtable, at(l2.Approval_Smoothed= 44 Incumbcode= 0  gov_party=2 dem_per_two_party=50.1568 pres_copartisan=0 l2.Approval_Smoothed_pres=39) statistics (est ul ll) below rown(Arizona) dec(2)
*Arkansas
quietly mtable, at(l2.Approval_Smoothed= 58 Incumbcode= 0  gov_party=2 dem_per_two_party=35.7876 pres_copartisan=0 l2.Approval_Smoothed_pres=32) statistics (est ul ll) below rown(Arkansas) dec(2)
*California 
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=1 dem_per_two_party=64.9089 pres_copartisan=1 l2.Approval_Smoothed_pres=52) statistics (est ul ll) below rown(California) dec(2)
*Colorado
quietly mtable, at(l2.Approval_Smoothed= 57 Incumbcode= 1  gov_party=1 dem_per_two_party=64.9089 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Colorado) dec(2)
*Connecticut
quietly mtable, at(l2.Approval_Smoothed= 58 Incumbcode= 1  gov_party=1 dem_per_two_party=60.1951 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Connecticut) dec(2)
*Florida
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=2 dem_per_two_party=48.3052 pres_copartisan=0 l2.Approval_Smoothed_pres=43) statistics (est ul ll) below rown(Florida) dec(2)
*Georgia 
quietly mtable, at(l2.Approval_Smoothed= 52 Incumbcode= 1  gov_party=2 dem_per_two_party=50.1193 pres_copartisan=0 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(Georgia) dec(2)
*Hawaii 
quietly mtable, at(l2.Approval_Smoothed= 44 Incumbcode= 0 gov_party=1 dem_per_two_party=65.0327 pres_copartisan=1 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Hawaii) dec(2)
*Idaho 
quietly mtable, at(l2.Approval_Smoothed= 54 Incumbcode= 1  gov_party=2 dem_per_two_party=34.1229 pres_copartisan=0 l2.Approval_Smoothed_pres=28) statistics (est ul ll) below rown(Idaho) dec(2)
*Illinois
quietly mtable, at(l2.Approval_Smoothed= 51 Incumbcode= 1  gov_party=1 dem_per_two_party=58.659 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Illinois) dec(2)
*Iowa 
quietly mtable, at(l2.Approval_Smoothed= 47 Incumbcode= 1  gov_party=2 dem_per_two_party=45.8167 pres_copartisan=0 l2.Approval_Smoothed_pres=36) statistics (est ul ll) below rown(Iowa) dec(2)
*Kansas 
quietly mtable, at(l2.Approval_Smoothed= 56 Incumbcode= 1  gov_party=1 dem_per_two_party=42.5066 pres_copartisan=1 l2.Approval_Smoothed_pres=36) statistics (est ul ll) below rown(Kansas)  dec(2)
*Maine 
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=1 dem_per_two_party=42.5066 pres_copartisan=1 l2.Approval_Smoothed_pres=43) statistics (est ul ll) below rown(Maine) dec(2)
*Maryland
quietly mtable, at(l2.Approval_Smoothed= 70 Incumbcode= 0  gov_party=2 dem_per_two_party=67.0291 pres_copartisan=0 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Maryland) dec(2)
*Massachusetts
quietly mtable, at(l2.Approval_Smoothed= 73 Incumbcode= 0  gov_party=2 dem_per_two_party=67.0291 pres_copartisan=0 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Massachusetts) dec(2)
*Michigan
quietly mtable, at(l2.Approval_Smoothed= 50 Incumbcode= 1  gov_party=1 dem_per_two_party=51.4136 pres_copartisan=1 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(Michigan) dec(2)
*Minnesota
quietly mtable, at(l2.Approval_Smoothed= 51 Incumbcode= 1  gov_party=1 dem_per_two_party=53.6395 pres_copartisan=1 l2.Approval_Smoothed_pres=44) statistics (est ul ll) below rown(Minnesota) dec(2)
*Nebraska
quietly mtable, at(l2.Approval_Smoothed= 51 Incumbcode= 0  gov_party=2 dem_per_two_party=40.216 pres_copartisan=0 l2.Approval_Smoothed_pres=30) statistics (est ul ll) below rown(Nebraska) dec(2)
*Nevada
quietly mtable, at(l2.Approval_Smoothed= 50 Incumbcode= 1  gov_party=1 dem_per_two_party=51.2231 pres_copartisan=1 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(Nevada) dec(2)
*New Hampshire
quietly mtable, at(l2.Approval_Smoothed= 60 Incumbcode= 1  gov_party=2 dem_per_two_party=53.7484 pres_copartisan=0 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(New Hampshire) dec(2)
*New Mexico
quietly mtable, at(l2.Approval_Smoothed= 49 Incumbcode= 1  gov_party=1 dem_per_two_party=53.7484 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(New Mexico) dec(2)
*New York
quietly mtable, at(l2.Approval_Smoothed= 49 Incumbcode= 2  gov_party=1 dem_per_two_party=61.7168 pres_copartisan=1 l2.Approval_Smoothed_pres=49) statistics (est ul ll) below rown(New York) dec(2)
*Ohio 
quietly mtable, at(l2.Approval_Smoothed= 57 Incumbcode= 1  gov_party=2 dem_per_two_party=45.9233 pres_copartisan=0 l2.Approval_Smoothed_pres=37) statistics (est ul ll) below rown(Ohio) dec(2)
*Oklahoma 
quietly mtable, at(l2.Approval_Smoothed= 48 Incumbcode= 1  gov_party=2 dem_per_two_party=33.06 pres_copartisan=0 l2.Approval_Smoothed_pres=28) statistics (est ul ll) below rown(Oklahoma)  dec(2)
*Oregon 
quietly mtable, at(l2.Approval_Smoothed= 40 Incumbcode= 0  gov_party=1 dem_per_two_party=58.3072 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Oregon) dec(2)
*Pennsylvania
quietly mtable, at(l2.Approval_Smoothed= 47 Incumbcode= 0  gov_party=1 dem_per_two_party=50.5892 pres_copartisan=1 l2.Approval_Smoothed_pres=41) statistics (est ul ll) below rown(Pennsylvania) dec(2)
*Rhode Island
quietly mtable, at(l2.Approval_Smoothed= 38 Incumbcode= 2  gov_party=1 dem_per_two_party=60.5994 pres_copartisan=1 l2.Approval_Smoothed_pres=44) statistics (est ul ll) below rown(Rhode Island) dec(2)
*South Carolina
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=2 dem_per_two_party=44.0734 pres_copartisan=0 l2.Approval_Smoothed_pres=37) statistics (est ul ll) below rown(South Carolina) dec(2)
*South Dakota
quietly mtable, at(l2.Approval_Smoothed= 59 Incumbcode= 1  gov_party=2 dem_per_two_party=36.5652 pres_copartisan=0 l2.Approval_Smoothed_pres=33) statistics (est ul ll) below rown(South Dakota) dec(2)
*Tennessee
quietly mtable, at(l2.Approval_Smoothed= 56 Incumbcode= 1  gov_party=2 dem_per_two_party=36.5652 pres_copartisan=0 l2.Approval_Smoothed_pres=31) statistics (est ul ll) below rown(Tennessee) dec(2)
*Texas
quietly mtable, at(l2.Approval_Smoothed= 50 Incumbcode= 1  gov_party=2 dem_per_two_party=47.1693 pres_copartisan=0 l2.Approval_Smoothed_pres=37) statistics (est ul ll) below rown(Texas) dec(2)
*Vermont
quietly mtable, at(l2.Approval_Smoothed= 74 Incumbcode= 1  gov_party=2 dem_per_two_party=68.2992 pres_copartisan=0 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Vermont) dec(2)
*Wisconsin
quietly mtable, at(l2.Approval_Smoothed= 46 Incumbcode= 1  gov_party=1 dem_per_two_party=50.3191 pres_copartisan=1 l2.Approval_Smoothed_pres=39) statistics (est ul ll) below rown(Wisconsin) dec(2)
*Wyoming 
mtable, at(l2.Approval_Smoothed= 74 Incumbcode= 1  gov_party=2 dem_per_two_party=27.5196 pres_copartisan=0 l2.Approval_Smoothed_pres=22) statistics (est ul ll) below rown(Wyoming) dec(2)

**Table A6 **
logit winner l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party c.l2.Approval_Smoothed_pres##pres_copartisan
*Alabama*
quietly mtable, at(l2.Approval_Smoothed= 57 Incumbcode= 1  gov_party=2 dem_per_two_party=37 pres_copartisan=0 l2.Approval_Smoothed_pres=32) statistics (est ul ll) rown(Alabama) dec(2)
*Alaska
quietly mtable, at(l2.Approval_Smoothed= 52 Incumbcode= 1  gov_party=2 dem_per_two_party=44.7 pres_copartisan=0 l2.Approval_Smoothed_pres=35) statistics (est ul ll) below rown(Alaska) dec(2)
*Arizona
quietly mtable, at(l2.Approval_Smoothed= 44 Incumbcode= 0  gov_party=2 dem_per_two_party=50.1568 pres_copartisan=0 l2.Approval_Smoothed_pres=39) statistics (est ul ll) below rown(Arizona) dec(2)
*Arkansas
quietly mtable, at(l2.Approval_Smoothed= 58 Incumbcode= 0  gov_party=2 dem_per_two_party=35.7876 pres_copartisan=0 l2.Approval_Smoothed_pres=32) statistics (est ul ll) below rown(Arkansas) dec(2)
*California 
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=1 dem_per_two_party=64.9089 pres_copartisan=1 l2.Approval_Smoothed_pres=52) statistics (est ul ll) below rown(California) dec(2)
*Colorado
quietly mtable, at(l2.Approval_Smoothed= 57 Incumbcode= 1  gov_party=1 dem_per_two_party=64.9089 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Colorado) dec(2)
*Connecticut
quietly mtable, at(l2.Approval_Smoothed= 58 Incumbcode= 1  gov_party=1 dem_per_two_party=60.1951 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Connecticut) dec(2)
*Florida
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=2 dem_per_two_party=48.3052 pres_copartisan=0 l2.Approval_Smoothed_pres=43) statistics (est ul ll) below rown(Florida) dec(2)
*Georgia 
quietly mtable, at(l2.Approval_Smoothed= 52 Incumbcode= 1  gov_party=2 dem_per_two_party=50.1193 pres_copartisan=0 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(Georgia) dec(2)
*Hawaii 
quietly mtable, at(l2.Approval_Smoothed= 44 Incumbcode= 0 gov_party=1 dem_per_two_party=65.0327 pres_copartisan=1 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Hawaii) dec(2)
*Idaho 
quietly mtable, at(l2.Approval_Smoothed= 54 Incumbcode= 1  gov_party=2 dem_per_two_party=34.1229 pres_copartisan=0 l2.Approval_Smoothed_pres=28) statistics (est ul ll) below rown(Idaho) dec(2)
*Illinois
quietly mtable, at(l2.Approval_Smoothed= 51 Incumbcode= 1  gov_party=1 dem_per_two_party=58.659 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Illinois) dec(2)
*Iowa 
quietly mtable, at(l2.Approval_Smoothed= 47 Incumbcode= 1  gov_party=2 dem_per_two_party=45.8167 pres_copartisan=0 l2.Approval_Smoothed_pres=36) statistics (est ul ll) below rown(Iowa) dec(2)
*Kansas 
quietly mtable, at(l2.Approval_Smoothed= 56 Incumbcode= 1  gov_party=1 dem_per_two_party=42.5066 pres_copartisan=1 l2.Approval_Smoothed_pres=36) statistics (est ul ll) below rown(Kansas)  dec(2)
*Maine 
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=1 dem_per_two_party=42.5066 pres_copartisan=1 l2.Approval_Smoothed_pres=43) statistics (est ul ll) below rown(Maine) dec(2)
*Maryland
quietly mtable, at(l2.Approval_Smoothed= 70 Incumbcode= 0  gov_party=2 dem_per_two_party=67.0291 pres_copartisan=0 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Maryland) dec(2)
*Massachusetts
quietly mtable, at(l2.Approval_Smoothed= 73 Incumbcode= 0  gov_party=2 dem_per_two_party=67.0291 pres_copartisan=0 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Massachusetts) dec(2)
*Michigan
quietly mtable, at(l2.Approval_Smoothed= 50 Incumbcode= 1  gov_party=1 dem_per_two_party=51.4136 pres_copartisan=1 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(Michigan) dec(2)
*Minnesota
quietly mtable, at(l2.Approval_Smoothed= 51 Incumbcode= 1  gov_party=1 dem_per_two_party=53.6395 pres_copartisan=1 l2.Approval_Smoothed_pres=44) statistics (est ul ll) below rown(Minnesota) dec(2)
*Nebraska
quietly mtable, at(l2.Approval_Smoothed= 51 Incumbcode= 0  gov_party=2 dem_per_two_party=40.216 pres_copartisan=0 l2.Approval_Smoothed_pres=30) statistics (est ul ll) below rown(Nebraska) dec(2)
*Nevada
quietly mtable, at(l2.Approval_Smoothed= 50 Incumbcode= 1  gov_party=1 dem_per_two_party=51.2231 pres_copartisan=1 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(Nevada) dec(2)
*New Hampshire
quietly mtable, at(l2.Approval_Smoothed= 60 Incumbcode= 1  gov_party=2 dem_per_two_party=53.7484 pres_copartisan=0 l2.Approval_Smoothed_pres=42) statistics (est ul ll) below rown(New Hampshire) dec(2)
*New Mexico
quietly mtable, at(l2.Approval_Smoothed= 49 Incumbcode= 1  gov_party=1 dem_per_two_party=53.7484 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(New Mexico) dec(2)
*New York
quietly mtable, at(l2.Approval_Smoothed= 49 Incumbcode= 2  gov_party=1 dem_per_two_party=61.7168 pres_copartisan=1 l2.Approval_Smoothed_pres=49) statistics (est ul ll) below rown(New York) dec(2)
*Ohio 
quietly mtable, at(l2.Approval_Smoothed= 57 Incumbcode= 1  gov_party=2 dem_per_two_party=45.9233 pres_copartisan=0 l2.Approval_Smoothed_pres=37) statistics (est ul ll) below rown(Ohio) dec(2)
*Oklahoma 
quietly mtable, at(l2.Approval_Smoothed= 48 Incumbcode= 1  gov_party=2 dem_per_two_party=33.06 pres_copartisan=0 l2.Approval_Smoothed_pres=28) statistics (est ul ll) below rown(Oklahoma)  dec(2)
*Oregon 
quietly mtable, at(l2.Approval_Smoothed= 40 Incumbcode= 0  gov_party=1 dem_per_two_party=58.3072 pres_copartisan=1 l2.Approval_Smoothed_pres=47) statistics (est ul ll) below rown(Oregon) dec(2)
*Pennsylvania
quietly mtable, at(l2.Approval_Smoothed= 47 Incumbcode= 0  gov_party=1 dem_per_two_party=50.5892 pres_copartisan=1 l2.Approval_Smoothed_pres=41) statistics (est ul ll) below rown(Pennsylvania) dec(2)
*Rhode Island
quietly mtable, at(l2.Approval_Smoothed= 38 Incumbcode= 2  gov_party=1 dem_per_two_party=60.5994 pres_copartisan=1 l2.Approval_Smoothed_pres=44) statistics (est ul ll) below rown(Rhode Island) dec(2)
*South Carolina
quietly mtable, at(l2.Approval_Smoothed= 53 Incumbcode= 1  gov_party=2 dem_per_two_party=44.0734 pres_copartisan=0 l2.Approval_Smoothed_pres=37) statistics (est ul ll) below rown(South Carolina) dec(2)
*South Dakota
quietly mtable, at(l2.Approval_Smoothed= 59 Incumbcode= 1  gov_party=2 dem_per_two_party=36.5652 pres_copartisan=0 l2.Approval_Smoothed_pres=33) statistics (est ul ll) below rown(South Dakota) dec(2)
*Tennessee
quietly mtable, at(l2.Approval_Smoothed= 56 Incumbcode= 1  gov_party=2 dem_per_two_party=36.5652 pres_copartisan=0 l2.Approval_Smoothed_pres=31) statistics (est ul ll) below rown(Tennessee) dec(2)
*Texas
quietly mtable, at(l2.Approval_Smoothed= 50 Incumbcode= 1  gov_party=2 dem_per_two_party=47.1693 pres_copartisan=0 l2.Approval_Smoothed_pres=37) statistics (est ul ll) below rown(Texas) dec(2)
*Vermont
quietly mtable, at(l2.Approval_Smoothed= 74 Incumbcode= 1  gov_party=2 dem_per_two_party=68.2992 pres_copartisan=0 l2.Approval_Smoothed_pres=51) statistics (est ul ll) below rown(Vermont) dec(2)
*Wisconsin
quietly mtable, at(l2.Approval_Smoothed= 46 Incumbcode= 1  gov_party=1 dem_per_two_party=50.3191 pres_copartisan=1 l2.Approval_Smoothed_pres=39) statistics (est ul ll) below rown(Wisconsin) dec(2)
*Wyoming 
mtable, at(l2.Approval_Smoothed= 74 Incumbcode= 1  gov_party=2 dem_per_two_party=27.5196 pres_copartisan=0 l2.Approval_Smoothed_pres=22) statistics (est ul ll) below rown(Wyoming) dec(2)
	
	
********************************************************************************
* Analysis of 2022 Results
********************************************************************************

use "2022 results data.dta", clear

* Calculate  errors
gen error_lasso = lassovote - results
gen error_pvi = pvi_forecast - results
gen error_538 = _538forecast - results

* Calculate absolute errors
gen abs_error_lasso = abs(results - lassovote)
gen abs_error_pvi = abs(results - pvi_forecast)
gen abs_error_538 = abs(results - _538forecast)

* Calculate squared errors
gen error_sq_lasso = (results - lassovote)^2
gen error_sq_pvi = (results - pvi_forecast)^2
gen error_sq_538 = (results - _538forecast)^2

**Table 2 MAE**
* Calculate error metrics
// Calculate and display square root of means for absolute errors
mean abs_error_lasso abs_error_538  abs_error_pvi

**Table 2 RMSE**
// Calculate and display square root of means for squared errors
mean error_sq_lasso error_sq_538 error_sq_pvi 
matrix B = e(b)
foreach var in error_sq_pvi error_sq_lasso error_sq_538 {
    local i = colnumb(B, "`var'")
    display "sqrt(mean) of `var': " sqrt(B[1,`i'])
}
**Table 2 MAE no MA, MD, HI**
* Calculate error metrics excluding high-leverage cases of MA, MD, HI
 // Calculate and display square root of means for absolute errors
mean abs_error_lasso abs_error_538  abs_error_pvi  if mass_mary_hi == 0

**Table 2 RMSE no MA, MD, HI**
// Calculate and display square root of means for squared errors
mean error_sq_lasso error_sq_538 error_sq_pvi if mass_mary_hi == 0
matrix B = e(b)
foreach var in error_sq_pvi error_sq_lasso error_sq_538 {
    local i = colnumb(B, "`var'")
    display "sqrt(mean) of `var': " sqrt(B[1,`i'])
}

********************************************************************************
* Visualizations
********************************************************************************

* Figure 2: Confidence Intervals and Results
twoway ///
    (rcap ll ul states if !missing(ul) & !missing(ll) & !missing(states), ///
        horizontal lcolor(orange) msize(small)) ///
    (scatter states lassovote if !missing(lassovote) & !missing(states), ///
        msymbol(circle) mcolor(black) msize(small)) ///
    (scatter states results if !missing(results) & !missing(states), ///
        msymbol(diamond) mcolor(gs10) msize(small)), ///
    yscale(reverse noline) ///
    ylabel(#35, valuelabel labsize(small) angle(0) grid glcolor(gs15) glwidth(thin)) ///
    xlabel(30(10)80, labsize(small) grid gstyle(minor) glcolor(gs15) glwidth(thin)) ///
    xtitle("Incumbent Party Vote") ///
    ytitle("") ///
    xline(50, lcolor(gs13) lwidth(thin)) ///
    legend(off) ///
    graphregion(color(white)) ///
    plotregion(color(white))

* First, create value labels for states
encode state_name, generate(state_id)

* Figure 3A: Error Comparison by State
twoway ///
    (scatter state_id error_538 if !missing(error_538), ///
        msymbol(circle) mcolor(blue) msize(small)) ///
    (scatter state_id error_lasso if !missing(error_lasso), ///
        msymbol(diamond) mcolor(orange) msize(small)) ///
    (scatter state_id error_pvi if !missing(error_pvi), ///
        msymbol(square) mcolor(gs10) msize(small)), ///
    yscale(reverse noline) ///
    ylabel(#35, valuelabel labsize(small) angle(0) noticks grid glcolor(gs15) glwidth(thin) labgap(0)) ///
    xlabel(-40(20)20, labsize(small) grid gstyle(minor) glcolor(gs15) glwidth(thin)) ///
    xline(0, lcolor(black) lwidth(thin)) ///
    legend(order(1 "Error: 538" 2 "Error: LASSO-Popularity" 3 "Error: Cook's PVI") ///
        rows(1) position(6) size(small) symxsize(small)) ///
    graphregion(color(white) margin(l=0)) ///
    plotregion(color(white) margin(l=0))


* Figure 3B: Absolute Error by Incumbent Party
graph dot (asis) abs_error_538 abs_error_lasso abs_error_pvi,  ///
    over(state_name, sort(incumbent_party) label(labsize(vsmall))) linetype(line) lines(lcolor(gs15)) ///
    marker(1, msymbol(circle) mcolor(blue) msize(small)) ///
    marker(2, msymbol(diamond) mcolor(orange) msize(small)) ///
    marker(3, msymbol(square) mcolor(gray) msize(small)) ///
	    legend(order(1 "Absolute Error: 538" ///
                 2 "Absolute Error: LASSO-Popularity" ///
                 3 "Absolute Error: Cook's PVI") ///
           rows(1) size(vsmall) region(lcolor(white))) ///
    text(-7 65 "G.O.P." 25 -7 "Democrat", orientation(vert) size(small))



********************************************************************************
* 2024 Vote Share Forecasts
********************************************************************************
**Table 3**
use "full data gov.dta", clear

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 

* Delaware
quietly mtable, at(l2.Approval_Smoothed=59 Incumbcode=0 gov_party=1 ///
    dem_per_two_party=60 pres_copartisan=1 l2.Approval_Smoothed_pres=44) ///
    statistics(est ul ll) rown(Delaware) dec(2)

* Indiana
quietly mtable, at(l2.Approval_Smoothed=52 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=42 pres_copartisan=0 l2.Approval_Smoothed_pres=33) ///
    statistics(est ul ll) rown(Indiana) dec(2) below

* Missouri
quietly mtable, at(l2.Approval_Smoothed=52 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=42 pres_copartisan=0 l2.Approval_Smoothed_pres=33) ///
    statistics(est ul ll) rown(Missouri) dec(2) below

* Montana
quietly mtable, at(l2.Approval_Smoothed=57 Incumbcode=1 gov_party=2 ///
    dem_per_two_party=42 pres_copartisan=0 l2.Approval_Smoothed_pres=35) ///
    statistics(est ul ll) rown(Montana) dec(2) below

* New Hampshire
quietly mtable, at(l2.Approval_Smoothed=63 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=54 pres_copartisan=0 l2.Approval_Smoothed_pres=43) ///
    statistics(est ul ll) rown(New Hampshire) dec(2) below

* North Carolina
quietly mtable, at(l2.Approval_Smoothed=55 Incumbcode=0 gov_party=1 ///
    dem_per_two_party=49 pres_copartisan=1 l2.Approval_Smoothed_pres=40) ///
    statistics(est ul ll) rown(North Carolina) dec(2) below

* North Dakota
quietly mtable, at(l2.Approval_Smoothed=59 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=33 pres_copartisan=0 l2.Approval_Smoothed_pres=25) ///
    statistics(est ul ll) rown(North Dakota) dec(2) below

* Utah
quietly mtable, at(l2.Approval_Smoothed=55 Incumbcode=1 gov_party=2 ///
    dem_per_two_party=39 pres_copartisan=0 l2.Approval_Smoothed_pres=30) ///
    statistics(est ul ll) rown(Utah) dec(2) below

* Vermont
quietly mtable, at(l2.Approval_Smoothed=81 Incumbcode=1 gov_party=2 ///
    dem_per_two_party=68 pres_copartisan=0 l2.Approval_Smoothed_pres=49) ///
    statistics(est ul ll) rown(Vermont) dec(2) below

* Washington
quietly mtable, at(l2.Approval_Smoothed=49 Incumbcode=0 gov_party=1 ///
    dem_per_two_party=60 pres_copartisan=1 l2.Approval_Smoothed_pres=49) ///
    statistics(est ul ll) rown(Washington) dec(2) below

* West Virginia
mtable, at(l2.Approval_Smoothed=63 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=30 pres_copartisan=0 l2.Approval_Smoothed_pres=24) ///
    statistics(est ul ll) rown(West Virginia) dec(2) below

********************************************************************************
* 2024 Winner Forecasts
********************************************************************************

logit winner l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 


* Delaware
quietly mtable, at(l2.Approval_Smoothed=59 Incumbcode=0 gov_party=1 ///
    dem_per_two_party=60 pres_copartisan=1 l2.Approval_Smoothed_pres=44) ///
    statistics(est ul ll) rown(Delaware) dec(2)

* Indiana
quietly mtable, at(l2.Approval_Smoothed=52 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=42 pres_copartisan=0 l2.Approval_Smoothed_pres=33) ///
    statistics(est ul ll) rown(Indiana) dec(2) below

* Missouri
quietly mtable, at(l2.Approval_Smoothed=52 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=42 pres_copartisan=0 l2.Approval_Smoothed_pres=33) ///
    statistics(est ul ll) rown(Missouri) dec(2) below

* Montana
quietly mtable, at(l2.Approval_Smoothed=57 Incumbcode=1 gov_party=2 ///
    dem_per_two_party=42 pres_copartisan=0 l2.Approval_Smoothed_pres=35) ///
    statistics(est ul ll) rown(Montana) dec(2) below

* New Hampshire
quietly mtable, at(l2.Approval_Smoothed=63 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=54 pres_copartisan=0 l2.Approval_Smoothed_pres=43) ///
    statistics(est ul ll) rown(New Hampshire) dec(2) below

* North Carolina
quietly mtable, at(l2.Approval_Smoothed=55 Incumbcode=0 gov_party=1 ///
    dem_per_two_party=49 pres_copartisan=1 l2.Approval_Smoothed_pres=40) ///
    statistics(est ul ll) rown(North Carolina) dec(2) below

* North Dakota
quietly mtable, at(l2.Approval_Smoothed=59 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=33 pres_copartisan=0 l2.Approval_Smoothed_pres=25) ///
    statistics(est ul ll) rown(North Dakota) dec(2) below

* Utah
quietly mtable, at(l2.Approval_Smoothed=55 Incumbcode=1 gov_party=2 ///
    dem_per_two_party=39 pres_copartisan=0 l2.Approval_Smoothed_pres=30) ///
    statistics(est ul ll) rown(Utah) dec(2) below

* Vermont
quietly mtable, at(l2.Approval_Smoothed=81 Incumbcode=1 gov_party=2 ///
    dem_per_two_party=68 pres_copartisan=0 l2.Approval_Smoothed_pres=49) ///
    statistics(est ul ll) rown(Vermont) dec(2) below

* Washington
quietly mtable, at(l2.Approval_Smoothed=49 Incumbcode=0 gov_party=1 ///
    dem_per_two_party=60 pres_copartisan=1 l2.Approval_Smoothed_pres=49) ///
    statistics(est ul ll) rown(Washington) dec(2) below

* West Virginia
mtable, at(l2.Approval_Smoothed=63 Incumbcode=0 gov_party=2 ///
    dem_per_two_party=30 pres_copartisan=0 l2.Approval_Smoothed_pres=24) ///
    statistics(est ul ll) rown(West Virginia) dec(2) below

********************************************************************************
* Appendix Analyses
* Values and tables in Appendices 1 and 2 of the supplemental materials are from 
* Singer (2023) and are reproduced, along with a link to the original source, 
* for the reader's convenience (https://doi.org/10.7910/DVN/I6VZ9Y)
********************************************************************************

**Apendix 3**

* Base Model
reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan 
**outreg2 using "appendix 3", noparen dec(3) label replace
cv_regress

**Table A4**
* Model with PHCI--Col 1
lasso2 vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.phci
lasso2, lic(ebic)

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.phci
outreg2 using "appendix 3", noparen dec(3) label append
cv_regress

* Model with Growth--Col 2
lasso2 vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.growth
lasso2, lic(ebic)

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.growth
outreg2 using "appendix 3", noparen dec(3) label append
cv_regress

* Model with Income--Col 3
lasso2 vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.income
lasso2, lic(ebic)

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.income
outreg2 using "appendix 3", noparen dec(3) label append
cv_regress

* Model with Unemployment (state and nationa)--	Col 4
lasso2 vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.unemployment_state l2.unemployment_national
lasso2, lic(ebic)

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.unemployment_state l2.unemployment_national
outreg2 using "appendix 3", noparen dec(3) label append
cv_regress

* Model with Relative Unemployment---Col 5
lasso2 vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.unemployment_relative
lasso2, lic(ebic)

reg vote_share l2.c.Approval_Smoothed##i.Incumbcode gov_party##c.dem_per_two_party ///
    c.l2.Approval_Smoothed_pres##pres_copartisan l2.unemployment_relative
outreg2 using "appendix 3", noparen dec(3) label append
cv_regress
